//
//  HomeVC.swift
//  AzaanSwift
//
//  Created by Azzan on 2018/3/4.
//  Copyright © 2018年 Azzan. All rights reserved.
//

import UIKit

class HomeVC: AZBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    //tabelView
    var arrMenu:Array<Any> = Array()
    // 定义一个字符串数组并初始化
    var imageArray : [String] = ["ad_00", "ad_01", "ad_02", "ad_03", "ad_04"]
    let countCol = 4;
    let countRow = 2;

    override func viewDidLoad() {
        super.viewDidLoad()
        self.getMenuData()
        self.setcollectionView();
        
        
    }
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }
    func getMenuData() {
        arrMenu = [["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"],["title":"社么","image":"icon_homepage_hotelCategory"]]
    }
    func setcollectionView() {
        
        //tableView注册cell
//        tableView.register(HomeVCCell.self, forCellReuseIdentifier: "reuseIdentifier")
//        tableView.register(UINib.init(nibName: "HomeSecondeCell", bundle: nil), forCellReuseIdentifier: "HomeSecondeCell")
//        tableView.register(UINib.init(nibName: "HomeThirdCell", bundle: nil), forCellReuseIdentifier: "HomeThirdCell")
        
//        collectionView.register(AZHomeHeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "AZHomeHeadView")
    
        
        self.collectionView.register(UINib.init(nibName: "AZHomeFirstCell", bundle: nil), forCellWithReuseIdentifier: "AZHomeFirstCell")
        self.collectionView.register(UINib.init(nibName: "AZSecendCell", bundle: nil), forCellWithReuseIdentifier: "AZSecendCell")
        self.collectionView.register(UINib.init(nibName: "AZHomeThirdCell", bundle: nil), forCellWithReuseIdentifier: "AZHomeThirdCell")
        self.collectionView.register(UINib.init(nibName: "AZHomefourthNewCell", bundle: nil), forCellWithReuseIdentifier: "AZHomefourthNewCell")
        self.collectionView.register(UINib.init(nibName: "AZHomeFifthNewCell", bundle: nil), forCellWithReuseIdentifier: "AZHomeFifthNewCell")
        self.collectionView.register(UINib.init(nibName: "AZHomeSectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "AZHomeSectionHeaderView")
        
    
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 5
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }else if section == 1{
           return 8
        }else if section == 2{
            return 1
        }else if section == 3{
            return 1
        }else if section == 4{
            return 10
        }else{
            return 1
        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0{
            let cell:AZHomeFirstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZHomeFirstCell", for: indexPath) as! AZHomeFirstCell
            cell.imageArray = ["http://p.lrlz.com/data/upload/mobile/special/s252/s252_05471521705899113.png",
                               "http://p.lrlz.com/data/upload/mobile/special/s303/s303_05442007678060723.png",
                               "http://p.lrlz.com/data/upload/mobile/special/s303/s303_05442007587372591.png",
                               "http://p.lrlz.com/data/upload/mobile/special/s303/s303_05442007388249407.png",
                               "http://p.lrlz.com/data/upload/mobile/special/s303/s303_05442007470310935.png"]
            return cell
        }else if indexPath.section == 1{
            let cell:AZSecendCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZSecendCell", for: indexPath) as!AZSecendCell
            return cell
        }else if indexPath.section == 2{
            let cell:AZHomeThirdCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZHomeThirdCell", for: indexPath) as! AZHomeThirdCell
            return cell
        }else if indexPath.section == 3{
            let cell:AZHomefourthNewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZHomefourthNewCell", for: indexPath) as! AZHomefourthNewCell
            return cell
        }else if indexPath.section == 4{
            let cell:AZHomeFifthNewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZHomeFifthNewCell", for: indexPath) as! AZHomeFifthNewCell
            return cell
        }
        else{
            let cell:AZSecendCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AZSecendCell", for: indexPath) as!AZSecendCell
            return cell
        }
    }
    //sectionHeader
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:UICollectionReusableView!
        //分区头
        if kind == UICollectionElementKindSectionHeader{
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                           withReuseIdentifier: "AZHomeSectionHeaderView", for: indexPath)
            //设置头部标题
//            let label = reusableview.viewWithTag(1) as! UILabel
//            label.text = books[indexPath.section].title
        }
            //分区尾
//        else if kind == UICollectionElementKindSectionFooter{
//            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
//                                                                           withReuseIdentifier: "FooterView", for: indexPath)
//
//        }
        return reusableview
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 || section == 1 {
            let headerSize = CGSize.init(width: KScreenW, height: CGFloat.leastNormalMagnitude)
            return headerSize
        }
        let headerSize = CGSize.init(width: KScreenW, height: 44)
        return headerSize
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0{
            let size = CGSize.init(width: KScreenW, height: KScreenW * 0.53)
            return size
        }else if indexPath.section == 1{
//            let width = KScreenW/4
            let size = CGSize.init(width: (KScreenW)/4, height: 100)
            return size
        }else if indexPath.section == 2{
            //            let width = KScreenW/4
            let size = CGSize.init(width: KScreenW, height: 100)
            return size
        }else if indexPath.section == 3{
            //            let width = KScreenW/4
            let size = CGSize.init(width: KScreenW, height: 198)
            return size
        }else if indexPath.section == 4{
            //            let width = KScreenW/4
            let size = CGSize.init(width: (KScreenW-2)/3, height: 148)
            return size
        }else{
            let size = CGSize.init(width: KScreenW, height: 100)
            return size
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let insets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        return insets
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        if section == 4 {
            return 0.5
        }
        return 0
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
//        <#code#>
//    }
    

    

}

